class CalController < ApplicationController

  before_filter :require_login, :only => ["book", "mydates"] 
  
  # verify :method => :post, :only => [ :book], :redirect_to => { :action => :browse }

  def browse
    if params[:id]
    	p = params[:id].split("-")
    	@year = p[0].to_i
    	@month = p[1].to_i
    else
    	@year = Date.today.year
    	@month = Date.today.month
    end
  end
  
  def view
    @day = params[:id]
  end
  
  def book
    # Date Objekt aus Parameter erstellen
    d = params[:id].split(/T|-|:/)
    @date_begin = Time.mktime(d[0], d[1], d[2], d[3], d[4])

    # Wenn GET-Request -> Confirmation Seite
    if request.get?
      render :action => "book"
      return
    end

    # Neuen Termin speichern
    @termin = Termin.new
    @termin.patient_id = session[:user][:id]
    @termin.date_begin = @date_begin
    @termin.save
    if @termin.errors.size > 0
      flash[:error] = "Fehler: Der Termin konnte nicht reserviert werden."
    else
      flash[:notice] = "Vielen Dank. Ihre Reservierung wurde vorgenommen."
    end
    redirect_to :action => "browse"
  end
  
  def mydates
    @termin = Termin.find_all_by_patient_id(session[:user][:id])
  end
end
